<?php

use common\models\c2\entity\Product;
use kartik\builder\Form;
use kartik\widgets\ActiveForm;
use kartik\widgets\TouchSpin;
use kzo\base\models\statics\EntityModelStatus;
use kzo\base\models\statics\OperationEvent;
use kzo\base\widgets\ui\bs4\Alert;
use unclead\multipleinput\MultipleInput;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\Pjax;

/** @var $model \common\models\c2\entity\ProductPackage */
/** @var $this \yii\web\View */

$regularLangName = \Yii::$app->kzoHelper->getRegularLangName();
$messageName = $model->getMessageName();

?>


<?php
$form = ActiveForm::begin([
    'action' => [
        'package/edit',
        'id' => $model->id,
        // 'type' => \common\models\c2\statics\AssembleType::TYPE_PACKAGE
    ],
    'options' => [
        'id' => $model->getBaseFormName(),
        'data-pjax' => true,
        'style' => ['padding' => '10px']
    ]]);
?>

<div class="<?= $model->getPrefixName('form') ?>">
    <div class="">

        <?php if (Yii::$app->session->hasFlash($messageName)): ?>
            <?php if (!$model->hasErrors()) {
                echo Alert::widget([
                    'type' => Alert::TYPE_SUCCESS,
                    'title' => 'Well Down',
                    'icon' => 'fas fa-ok-circle',
                    'body' => Yii::$app->session->getFlash($messageName),
                    'showSeparator' => true,
                    'delay' => 2000
                ]);
                $this->registerJs(
                // "jQuery('#refresh-btn').trigger('" . OperationEvent::REFRESH . "');"
                    "jQuery('#product-package-grid-fresh').click();"
                );
            } else {
                echo Alert::widget([
                    'type' => Alert::TYPE_WARNING,
                    'title' => 'Warning!',
                    'icon' => 'fas fa-ok-circle',
                    'body' => Yii::$app->session->getFlash($messageName),
                    'showSeparator' => true,
                ]);
            }
            ?>
        <?php endif; ?>

        <?php

        echo Form::widget([
            'model' => $model,
            'form' => $form,
            'columns' => 4,
            'attributes' => [
                'label' => [
                    'type' => Form::INPUT_TEXT,
                    'options' => [
                        'placeholder' => $model->getAttributeLabel('label')
                    ]
                ],
                'capacity' => [
                    'type' => Form::INPUT_TEXT,
                    'options' => [
                        'type' => 'number',
                        'placeholder' => $model->getAttributeLabel('capacity')
                    ]
                ],
                'unit' => [
                    'type' => Form::INPUT_TEXT,
                    'options' => [
                        'placeholder' => $model->getAttributeLabel('unit')
                    ]
                ],
                'weight' => [
                    'type' => Form::INPUT_TEXT,
                    'options' => [
                        'placeholder' => $model->getAttributeLabel('weight')
                    ]
                ],
                'volume' => [
                    'type' => Form::INPUT_TEXT,
                    'options' => [
                        'placeholder' => $model->getAttributeLabel('volume')
                    ]
                ],
                'status' => ['type' => Form::INPUT_DROPDOWN_LIST, 'items' => EntityModelStatus::getHashMap('id', 'label')],
                'product_id' => [
                    'type' => Form::INPUT_HIDDEN,
                    'options' => [
                        'placeholder' => $model->getAttributeLabel('product_id')
                    ]
                ],
                'type' => [
                    'type' => Form::INPUT_HIDDEN,
                    'options' => [
                        'value' => 1,
                        'placeholder' => $model->getAttributeLabel('type')
                    ]
                ],
            ]
        ]);

        $multipleItemsId = $model->getPrefixName('items');

        echo Form::widget([
            'model' => $model,
            'form' => $form,
            'columns' => 1,
            'attributes' => [
                'items' => [
                    'type' => Form::INPUT_WIDGET,
                    'widgetClass' => unclead\multipleinput\MultipleInput::className(),
                    'options' => [
                        'addButtonOptions' => [
                            'class' => 'btn btn-success',
                            'label' => Html::tag('i', null, ['class' => 'fa fa-plus'])
                        ],
                        'removeButtonOptions' => [
                            'class' => 'btn btn-danger',
                            'label' => Html::tag('i', null, ['class' => 'fa fa-minus'])
                        ],
                        'data' => $model->items,
                        // 'max' => 4,
                        'allowEmptyList' => true,
                        'rowOptions' => function ($model, $index, $context) {
                            return ['data-id' => $model['id']];
                        },
                        'columns' => [
                            [
                                'name' => 'id',
                                'type' => 'hiddenInput',
                            ],
                            // [
                            //     'name' => 'label',
                            //     'title' => Yii::t('app.c2', 'Label'),
                            //     'enableError' => true,
                            //     'options' => [
                            //         'class' => 'input-priority'
                            //     ]
                            // ],
                            [
                                'name' => 'product_id',
                                'type' => \kartik\select2\Select2::className(),
                                'title' => Yii::t('app.c2', 'Product'),
                                'enableError' => true,
                                'options' => [
                                    // 'pluginOptions' => ['width' => '200px'],
                                    'class' => 'input-priority',
                                    'data' => ['' => 'selecting...'] + \common\models\c2\entity\Assemble::getHashMap('id', 'name', [
                                            'type' => \common\models\c2\statics\ProductType::TYPE_ASSEMBLE,
                                            'status' => EntityModelStatus::STATUS_ACTIVE
                                        ]),
                                ]
                            ],
                            [
                                'name' => 'need_num',
                                'title' => Yii::t('app.c2', 'Need Num'),
                                'enableError' => true,
                                'options' => [
                                    'type' => 'number',
                                    'class' => 'input-priority'
                                ]
                            ],
                            [
                                'name' => 'position',
                                'type' => kartik\widgets\TouchSpin::className(),
                                'title' => Yii::t('app.c2', 'Position'),
                                'defaultValue' => 50,
                            ],
                        ]
                    ],
                ],
            ]
        ]);


        echo Html::submitButton('<i class="fa fa-save"></i> ' . Yii::t('app.c2', 'Save'), ['type' => 'button', 'class' => 'btn btn-primary float-right ml-2']);
        // echo Html::a('<i class="fa fa-window-close"></i> ' . Yii::t('app.c2', 'Close'), ['default/index'], ['data-pjax' => '0', 'data-dismiss' => 'modal', 'class' => 'btn btn-light float-right', 'title' => Yii::t('app.c2', 'Go Back'),]);
        ?>

    </div>
</div>
<?php ActiveForm::end(); ?>

<?php
$js = "";
$js .= "jQuery('.btn.multiple-input-list__btn.js-input-remove').off('click').on('click', function(){
    var itemId = $(this).closest('tr').data('id');
    if(itemId){
       $.ajax({url:'" . Url::toRoute('package/delete-subitem') . "',data:{id:itemId}}).done(function(result){;}).fail(function(result){alert(result);});
    }
});\n";
$this->registerJs($js);
?>
